<template>
  <el-dialog
    title="积分通用配置"
    :visible.sync="dialogVisible"
    :show="show"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    top="20vh"
    @open='open'
    @close="$emit('update:show',false)"
  >
    <el-form style="margin:50px 70px 50px 40px;" label-width="150px" :model="ruleForm" :rules="rules" ref="ruleForm" >
        <el-form-item label="发邮件消耗积分:"  prop="sendEmailPoints">
          <el-input
            placeholder="请输入每封邮件的消耗的积分"
            v-model="ruleForm.sendEmailPoints"
            maxlength="5"
            show-word-limit
            clearable>
             <template slot="append">积分/封</template>
          </el-input>
          <el-tag type="warning" style="width:100%">积 分 比 例 ：1 元 人 民 币 = 100 积 分</el-tag>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose()">取 消</el-button>
        <el-button type="primary" @click="submitEditEmailPointsConfig('ruleForm')">确 定</el-button>
      </span>
  </el-dialog>
</template>

<style scoped>
/deep/ .el-dialog {
  min-width: 460px;
  max-width: 600px;
}

/deep/ .el-dialog__body {
  padding: 0;
}

.el-dialog-div {
  padding: 20px 20px 0 20px;
  min-width: 400px;
}
</style>
<script>
import Pagination from '@/components/Pagination'
import { getAction, postAction } from '@/api/common'
export default {
  props: {
    show: {
      type: Boolean,
      default: false
    },
    companyId: {
      type: [Number, String],
      default: ''
    }
  },
  data() {
    return {
      list: null,
      ruleForm: {
        SendEmailPoints: 0
      },
      dialogVisible: this.show,
      infoLoding: true,
      rules: {
        sendEmailPoints: [
          { required: true, message: '请输入每封邮件的消耗的积分', trigger: 'blur' }
        ]
      }
    }
  },
  components: {
  },
  methods: {
    async open() {
      this.infoLoding = true
      const { data } = await getAction(this.listQuery, 'CompanyPointsConfig/GetPointsGeneralConfiguration')
      this.ruleForm = data
      this.infoLoding = false
    },
    submitEditEmailPointsConfig(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          postAction(this.ruleForm, 'CompanyPointsConfig/UpdatePointsGeneralConfiguration')
            .then((res) => {
              if (res.code === 20000) {
                this.$message({
                  message: '修改积分通用配置成功!',
                  type: 'success'
                })
                this.ruleForm = {
                  SendEmailPoints: undefined
                }
                this.handleClose(false)
                this.$emit('closeAction')
              } else {
                this.$message({
                  message: res.msg,
                  type: 'warning'
                })
              }
            })
        } else {
          return false
        }
      })
    },
    handleClose() {
      this.resetForm('ruleForm')
      this.$emit('update:show', false)
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  },
  watch: {
    show() {
      this.dialogVisible = this.show
    }
  }
}
</script>
